using SqlSugar;
using Rabbit3.Api.Models;
using Rabbit3.Api.Dto;
using AutoMapper;

namespace Rabbit3.Api.Services
{
    /// <summary>
    /// 养殖信息服务接口
    /// </summary>
    public interface IFarmingService
    {
        /// <summary>
        /// 根据溯源信息ID获取养殖信息
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>养殖信息</returns>
        Task<FarmingResponseDto?> GetByTraceabilityIdAsync(int traceabilityId);

        /// <summary>
        /// 根据ID获取养殖信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>养殖信息</returns>
        Task<FarmingResponseDto?> GetByIdAsync(int id);

        /// <summary>
        /// 创建养殖信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        Task<FarmingResponseDto> CreateAsync(CreateFarmingDto createDto);

        /// <summary>
        /// 更新养殖信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        Task<FarmingResponseDto?> UpdateAsync(UpdateFarmingDto updateDto);

        /// <summary>
        /// 删除养殖信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteAsync(int id);

        /// <summary>
        /// 创建免疫信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        Task<ImmunityResponseDto> CreateImmunityAsync(CreateImmunityDto createDto);

        /// <summary>
        /// 更新免疫信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        Task<ImmunityResponseDto?> UpdateImmunityAsync(UpdateImmunityDto updateDto);

        /// <summary>
        /// 删除免疫信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteImmunityAsync(int id);

        /// <summary>
        /// 创建企业信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        Task<EnterpriseResponseDto> CreateEnterpriseAsync(CreateEnterpriseDto createDto);

        /// <summary>
        /// 更新企业信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        Task<EnterpriseResponseDto?> UpdateEnterpriseAsync(UpdateEnterpriseDto updateDto);

        /// <summary>
        /// 删除企业信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteEnterpriseAsync(int id);
    }

    /// <summary>
    /// 养殖信息服务实现
    /// </summary>
    public class FarmingService : IFarmingService
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper _mapper;

        public FarmingService(ISqlSugarClient db, IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }

        /// <summary>
        /// 根据溯源信息ID获取养殖信息
        /// </summary>
        /// <param name="traceabilityId">溯源信息ID</param>
        /// <returns>养殖信息</returns>
        public async Task<FarmingResponseDto?> GetByTraceabilityIdAsync(int traceabilityId)
        {
            var entity = await _db.Queryable<FarmingInfo>()
                .Where(x => x.TraceabilityId == traceabilityId && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            var response = _mapper.Map<FarmingResponseDto>(entity);

            // 获取免疫信息
            var immunityInfos = await _db.Queryable<ImmunityInfo>()
                .Where(x => x.FarmingInfoId == entity.Id && !x.IsDeleted)
                .OrderBy(x => x.FarmingDays)
                .ToListAsync();

            response.ImmunityInfos = _mapper.Map<List<ImmunityResponseDto>>(immunityInfos);

            // 获取企业信息
            var enterpriseInfo = await _db.Queryable<EnterpriseInfo>()
                .Where(x => x.TraceabilityId == traceabilityId && x.StageType == "养殖" && !x.IsDeleted)
                .FirstAsync();

            if (enterpriseInfo != null)
            {
                response.EnterpriseInfo = _mapper.Map<EnterpriseResponseDto>(enterpriseInfo);
            }

            return response;
        }

        /// <summary>
        /// 根据ID获取养殖信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>养殖信息</returns>
        public async Task<FarmingResponseDto?> GetByIdAsync(int id)
        {
            var entity = await _db.Queryable<FarmingInfo>()
                .Where(x => x.Id == id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            var response = _mapper.Map<FarmingResponseDto>(entity);

            // 获取免疫信息
            var immunityInfos = await _db.Queryable<ImmunityInfo>()
                .Where(x => x.FarmingInfoId == entity.Id && !x.IsDeleted)
                .OrderBy(x => x.FarmingDays)
                .ToListAsync();

            response.ImmunityInfos = _mapper.Map<List<ImmunityResponseDto>>(immunityInfos);

            // 获取企业信息
            var enterpriseInfo = await _db.Queryable<EnterpriseInfo>()
                .Where(x => x.TraceabilityId == entity.TraceabilityId && x.StageType == "养殖" && !x.IsDeleted)
                .FirstAsync();

            if (enterpriseInfo != null)
            {
                response.EnterpriseInfo = _mapper.Map<EnterpriseResponseDto>(enterpriseInfo);
            }

            return response;
        }

        /// <summary>
        /// 创建养殖信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        public async Task<FarmingResponseDto> CreateAsync(CreateFarmingDto createDto)
        {
            var entity = _mapper.Map<FarmingInfo>(createDto);
            
            var id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
            entity.Id = id;

            return _mapper.Map<FarmingResponseDto>(entity);
        }

        /// <summary>
        /// 更新养殖信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        public async Task<FarmingResponseDto?> UpdateAsync(UpdateFarmingDto updateDto)
        {
            var entity = await _db.Queryable<FarmingInfo>()
                .Where(x => x.Id == updateDto.Id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            _mapper.Map(updateDto, entity);
            
            await _db.Updateable(entity).ExecuteCommandAsync();

            return _mapper.Map<FarmingResponseDto>(entity);
        }

        /// <summary>
        /// 删除养殖信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var result = await _db.Updateable<FarmingInfo>()
                .SetColumns(x => new FarmingInfo { IsDeleted = true, UpdatedAt = DateTime.Now })
                .Where(x => x.Id == id && !x.IsDeleted)
                .ExecuteCommandAsync();

            return result > 0;
        }

        /// <summary>
        /// 创建免疫信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        public async Task<ImmunityResponseDto> CreateImmunityAsync(CreateImmunityDto createDto)
        {
            var entity = _mapper.Map<ImmunityInfo>(createDto);
            
            var id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
            entity.Id = id;

            return _mapper.Map<ImmunityResponseDto>(entity);
        }

        /// <summary>
        /// 更新免疫信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        public async Task<ImmunityResponseDto?> UpdateImmunityAsync(UpdateImmunityDto updateDto)
        {
            var entity = await _db.Queryable<ImmunityInfo>()
                .Where(x => x.Id == updateDto.Id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            _mapper.Map(updateDto, entity);
            
            await _db.Updateable(entity).ExecuteCommandAsync();

            return _mapper.Map<ImmunityResponseDto>(entity);
        }

        /// <summary>
        /// 删除免疫信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        public async Task<bool> DeleteImmunityAsync(int id)
        {
            var result = await _db.Updateable<ImmunityInfo>()
                .SetColumns(x => new ImmunityInfo { IsDeleted = true, UpdatedAt = DateTime.Now })
                .Where(x => x.Id == id && !x.IsDeleted)
                .ExecuteCommandAsync();

            return result > 0;
        }

        /// <summary>
        /// 创建企业信息
        /// </summary>
        /// <param name="createDto">创建DTO</param>
        /// <returns>创建结果</returns>
        public async Task<EnterpriseResponseDto> CreateEnterpriseAsync(CreateEnterpriseDto createDto)
        {
            var entity = _mapper.Map<EnterpriseInfo>(createDto);
            
            var id = await _db.Insertable(entity).ExecuteReturnIdentityAsync();
            entity.Id = id;

            return _mapper.Map<EnterpriseResponseDto>(entity);
        }

        /// <summary>
        /// 更新企业信息
        /// </summary>
        /// <param name="updateDto">更新DTO</param>
        /// <returns>更新结果</returns>
        public async Task<EnterpriseResponseDto?> UpdateEnterpriseAsync(UpdateEnterpriseDto updateDto)
        {
            var entity = await _db.Queryable<EnterpriseInfo>()
                .Where(x => x.Id == updateDto.Id && !x.IsDeleted)
                .FirstAsync();

            if (entity == null)
                return null;

            _mapper.Map(updateDto, entity);
            
            await _db.Updateable(entity).ExecuteCommandAsync();

            return _mapper.Map<EnterpriseResponseDto>(entity);
        }

        /// <summary>
        /// 删除企业信息
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>删除结果</returns>
        public async Task<bool> DeleteEnterpriseAsync(int id)
        {
            var result = await _db.Updateable<EnterpriseInfo>()
                .SetColumns(x => new EnterpriseInfo { IsDeleted = true, UpdatedAt = DateTime.Now })
                .Where(x => x.Id == id && !x.IsDeleted)
                .ExecuteCommandAsync();

            return result > 0;
        }
    }
}


